package com.bjqh.edu.UI.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Orderdetails;
import com.bjqh.edu.R;
import com.bjqh.edu.adapter.CumulativeClassAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/9/15.
 */

public class CanceledFragment extends ListFragment implements SwipeRefreshLayout.OnRefreshListener{
    @BindView(android.R.id.list)
    ListView list;
    Unbinder unbinder;
    @BindView(android.R.id.empty)
    LinearLayout empty;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private CumulativeClassAdapter adapter;
    private List<Orderdetails.DataBean> dataBeen;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_canceled, null);
        ButterKnife.bind(this, view);
        unbinder = ButterKnife.bind(this, view);
        list.setEmptyView(empty);
        swipeLayout.setColorSchemeResources(R.color.grean);
        swipeLayout.setOnRefreshListener(this);
        getValue();
        return view;
    }

    private void getValue() {
        OkGo.init(getActivity().getApplication());
        OkGo.post(AppConstants.BASE_URL + "/order/datalist.shtml?")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("status", "1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (swipeLayout.isRefreshing())
                        {
                            swipeLayout.setRefreshing(false);
                        }

                        Log.i("已支付————", s);
                        Gson gson = new Gson();
                        Orderdetails orderdetails = gson.fromJson(s, Orderdetails.class);
                        dataBeen = orderdetails.getData();
                        if (dataBeen != null) {
                            adapter = new CumulativeClassAdapter(getContext(), dataBeen);
                            list.setAdapter(adapter);
                        }

                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        getValue();
    }
}
